package com.tanhao.takeaway.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhao.takeaway.commom.R;
import com.tanhao.takeaway.entity.*;
import com.tanhao.takeaway.service.categoryService;
import com.tanhao.takeaway.service.dishService;
import com.tanhao.takeaway.service.setmealDishService;
import com.tanhao.takeaway.service.setmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
public class setmealController {
    @Autowired
    private setmealDishService setmealdishservice;

    @Autowired
    private setmealService setmealservice;


    @Autowired
    private categoryService categoryservice;

    @PostMapping
    @CacheEvict(value = "setmeal",allEntries = true)
    public R<String> save(@RequestBody setmealDto setmealdto){
          setmealservice.saveWithDish(setmealdto);
          return R.success("新增套餐成功");

    }

    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        Page<setmeal> pageInfo=new Page<>(page,pageSize);
        Page<setmealDto> pageDto=new Page<>();
        LambdaQueryWrapper<setmeal> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(name!=null,setmeal::getName,name);
        lambdaQueryWrapper.orderByDesc(setmeal::getUpdateTime);

        setmealservice.page(pageInfo,lambdaQueryWrapper);
        BeanUtils.copyProperties(page,pageDto,"records");
        List<setmeal> records = pageInfo.getRecords();
        List<setmealDto> collect = records.stream().map((item) -> {
                    setmealDto dto = new setmealDto();
                    BeanUtils.copyProperties(item, dto);

                    Long categoryId = item.getCategoryId();
                    category c = categoryservice.getById(categoryId);
                    if (c != null)
                        dto.setCategoryName(c.getName());
                    return dto;
                }
        ).collect(Collectors.toList());
        pageDto.setRecords(collect);


        return R.success(pageDto);
    }

    @DeleteMapping
    @CacheEvict(value = "setmeal",allEntries = true)
    public R<String> delete(@RequestParam("ids") List<Long> ids){
        setmealservice.deleteWithDish(ids);
        return R.success("删除套餐成功");
    }


    @GetMapping("/list")
    @Cacheable(value = "setmeal",key="#s.categoryId+'_'+#s.status")
    public R<List<setmeal>> list(setmeal s){
        LambdaQueryWrapper<setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(s.getCategoryId() != null,setmeal::getCategoryId,s.getCategoryId());
        queryWrapper.eq(s.getStatus() != null,setmeal::getStatus,s.getStatus());
        queryWrapper.orderByDesc(setmeal::getUpdateTime);

        List<setmeal> list = setmealservice.list(queryWrapper);

        return R.success(list);
    }
    }

